<template>
  <div class="wrap">
    <van-tabs v-model:active="active" @click-tab="onClickTab(active)">
      <!-- 1 -->
      <van-tab title="申请记录">
        <div v-if="apply_ing_arr.length == 0" class="noData">
          <img src="./../../assets/img/noData.png" alt="" />
          <div class="txt">暂无记录</div>
        </div>
        <div v-else class="ul">
          <van-swipe-cell
            :right-width="rightWidth"
            class="message-li"
            v-for="(item, index) in apply_ing_arr"
            :key="index"
            :id="item.group_id"
          >
            <div class="content">
              <div class="li-right">
                <div class="name">群名称：{{ item.group_name }}</div>
                <div class="desc">昵称：{{ item.desc }}</div>
              </div>
              <div class="status APPLYING" v-if="item.status == 'APPLYING'">
                申请中
              </div>
              <div
                class="status APPROVED"
                v-else-if="item.status == 'APPROVED'"
              >
                已批准
              </div>
              <div class="status REFUSED" v-else-if="item.status == 'REFUSED'">
                已拒绝
              </div>
            </div>
            <!-- <template #right>
              <van-button
                square
                type="danger"
                text="删除"
                :stop-propagation="true"
                @click="onClose(item.group_id, 'applying')"
              />
            </template> -->
          </van-swipe-cell>
        </div>
      </van-tab>
      <!-- 2 -->
      <van-tab title="已加入的群组"
        ><div v-if="apply_ed_arr.length == 0" class="noData">
          <img src="./../../assets/img/noData.png" alt="" />
          <div class="txt">暂无群组</div>
        </div>
        <div v-else class="ul">
          <van-swipe-cell
            :right-width="rightWidth"
            class="message-li"
            v-for="(item, index) in apply_ed_arr"
            :key="index"
            :id="item.group_id"
          >
            <div class="content">
              <div class="li-right">
                <div class="name">群名称：{{ item.name }}</div>
                <div class="desc">昵称：{{ item.nick_name }}</div>
              </div>
            </div>
            <template #right>
              <van-button
                square
                type="danger"
                text="退群"
                :stop-propagation="true"
                @click="onClose(item.group_id, 'applyed')"
              />
            </template>
          </van-swipe-cell></div
      ></van-tab>
    </van-tabs>

    <div class="btn-wrap">
      <button class="btn" @click="goApply">扫码入群</button>
    </div>
  </div>
</template>

<script setup>
import { onMounted, ref, watch, reactive } from 'vue'
import { useRoute } from 'vue-router'
import { showSuccessToast, showFailToast, showToast } from 'vant'
import { postData } from '@/request/request'

let route = useRoute()
let query = route.query
localStorage.clear()
localStorage.setItem('user_id', query.user_id || '')
localStorage.setItem('access_token', query.access_token || '')
localStorage.setItem('open_id', query.open_id || '')
localStorage.setItem('currentPath', query.currentPath || '')

const rightWidth = ref(60)
const apply_ing_arr = ref([])
const apply_ed_arr = ref([])
const active = ref(0)

onMounted(() => {
  initList1()
})

const onClickTab = (activeNum) => {
  if (activeNum == 0) {
    initList1()
  } else {
    initList2()
  }
}
const initList1 = () => {
  let user_carried_id = !query.carriedUserid ? '' : query.carriedUserid
  postData('/user/group/applying/list', {
    user_id: user_carried_id,
  })
    .then((res) => {
      if (res.code == 0) {
        apply_ing_arr.value = res?.datas?.applyings || []
      } else {
        showToast(res.msg)
      }
    })
    .catch((err) => {
      console.log(err)
    })
}
const initList2 = () => {
  let user_carried_id = !query.carriedUserid ? '' : query.carriedUserid
  postData('/user/group/list/added', {
    user_id: user_carried_id,
  })
    .then((res) => {
      if (res.code == 0) {
        apply_ed_arr.value = res?.datas?.groups || []
      } else {
        showToast(res.msg)
      }
    })
    .catch((err) => {
      console.log(err)
    })
}

//左滑-退出群组
let onClose = (id, txt) => {
  let user_carried_id = !query.carriedUserid ? '' : query.carriedUserid
  postData('/user/group/remove', {
    user_id: user_carried_id,
    group_id: id,
  })
    .then((res) => {
      if (res.code == 0) {
        showToast('已退群')
        if (txt == 'applying') {
          initList1()
        } else if (txt == 'applyed') initList2()
      }
    })
    .catch((err) => {
      console.log(err)
    })
}

let goApply = () => {
  wx &&
    wx.miniProgram.navigateTo({
      url: `/pagesC/myGroup/apply?carriedUserid=${query.carriedUserid}`,
    })
}

watch(
  () => route.fullPath,
  () => {
    initList1()
  }
)
</script>

<style scoped>
.wrap {
  width: 100%;
  height: 100vh;
  background: #f7f7f8;
  font-size: 0.16rem;
  color: #333;
}

.noData {
  display: flex;
  flex-flow: column;
  justify-content: flex-start;
  align-items: center;
  margin-top: 0.5rem;
  margin-bottom: 0.5rem;
}
.noData img {
  width: 0.6rem;
  height: 0.6rem;
}
.noData .txt {
  margin-top: 0.1rem;
  font-size: 0.14rem;
  color: #8d8f91;
}
.ul {
  width: 100%;
  height: auto;
  margin: 0;
  padding: 0;
  display: flex;
  flex-flow: column;
  justify-content: flex-start;
  align-items: center;
  color: #333;
  font-size: 0.16rem;
}
.van-swipe-cell {
  width: 100%;
  height: 100%;
}

.message-li .van-swipe-cell__wrapper {
  width: 100%;
  height: 100% !important;
}
/* :deep .message-li .van-swipe-cell__right {
  height: 60% !important;
  display: flex;
  justify-content: flex-end;
  align-items: center;
} */
:deep .van-tabs--line .van-tabs__wrap {
  height: 0.6rem;
}
:deep .van-tabs__line {
  background: #d5ad57;
  width: 0.7rem;
}
:deep .van-tab__text--ellipsis {
  font-size: 0.16rem;
}
:deep .van-swipe-cell__right {
  top: auto;
}
.message-li {
  height: auto;
  background: #fff;
  margin: 0.05rem 0;
  width: 100%;
  box-sizing: border-box;
  position: relative;
}
.content {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  width: 100%;
  height: 100%;
  padding: 0.2rem;
}
.li-left {
  display: flex;
  justify-content: center;
  align-items: center;
}
.li-left img {
  width: 0.4rem;
  height: 0.4rem;
  border-radius: 50%;
}
.li-right {
  height: 100%;
  flex: 1;
  box-sizing: border-box;
  display: flex;
  flex-flow: column;
  justify-content: center;
  align-items: flex-start;
  font-size: 0.16rem;
}

.li-right .box .txt {
  /* margin-left: 0.075rem; */
}
.li-right .name {
  height: 0.25rem;
  line-height: 0.25rem;
  color: rgba(26, 27, 29, 1);
}
.li-right .desc {
  height: 0.25rem;
  line-height: 0.25rem;
  color: #676a6d;
  /* color: #d1aa55; */
}
.li-right .time {
  height: 0.25rem;
  line-height: 0.25rem;
  color: #676a6d;
}
.status {
  font-size: 0.14rem;
}
.APPLYING {
  color: #d18a17;
}
.APPROVED {
  color: green;
}
.REFUSED {
  color: #cd1313;
}

.van-swipe-cell {
  transition: transform 0.3s; /* 确保过渡效果适当 */
}
.btn-wrap {
  width: 90%;
  height: auto;
  display: flex;
  justify-content: center;
  align-items: center;
  position: fixed;
  bottom: 0.4rem;
  left: 5%;
}
.btn {
  width: 100%;
  height: 0.4rem;
  border-radius: 0.08rem;
  margin: 0 auto;
  font-size: 0.16rem;
  border-radius: 2rem;
  outline: none;
  border: none;
  color: #674102;
  text-shadow: 0 0.01rem 0.01rem #a7813d;
  cursor: pointer;
  background: linear-gradient(#d4a863, #f9d781);
  box-shadow: 0 0.2em 0 #d4a863, 0 0.2em 0.2em #f9d781;
}
</style>
